local fs  = require "nixio.fs"
local sys = require "luci.sys"
local utl = require "luci.util"
local ds =  require "luci.dispatcher"
local http = require "luci.http"
local pt = require "luci.http.protocol"
local nt = require "luci.model.nac_nsutil"

local nac_run   =  1 

m = SimpleForm("user_cfg", translate("NAC online users"),translate("Online user list"))
m.reset = false
m.submit = false



-- 进行查询，并将查询结果传入到view

local ubusrt = utl.ubus("nac_proxy", "websrv", {act='getonlineusernum',data={}}) or {msg="ubus error",rt_code="0"}

local rt_code = ubusrt["rt_code"] or ""
if rt_code ~= 200 then
   -- nt.error_render(m, rt_code, ubusrt["msg"])
   nac_run  =  0
   -- return m
end

local msg = ubusrt["msg"] or ""
if type(msg) ~= "table" then
    --不是table，表示服务器存在问题
   --nt.error_render(m, rt_code, translate("Server Error"))
   nac_run  =  0
   -- return m
end
--[[
local user_list = {}
for k,v in pairs(msg.users) do
    table.insert(user_list, v)
end
]]--
local user_list = {}
s = m:section(SimpleSection)
s.template = "cbi/blanksection"


-- alert控件
op = m:field(Button, "dropuser", translate("Drop"))
msg_code = http.formvalue("msg_code")
error_msg = ""
if msg_code == "1" then
    error_msg = "User off-line"
end
if #error_msg > 0 then 
    op.rt_code = msg_code
else
    op.rt_code = http.formvalue("code") or 200
end

--op.error_msg = translate(error_msg)
op.template  = "nac/alert"
if(nac_run == 0) then
	  t = m:section(Table, user_list, translate(translate("WAN is Not IP  Address!")))
	  return m
else
t = m:section(Table, user_list, translate(translate("NAC number of online users :") .. msg.staNum or "0"))
end
t.anonymous = true
t.addremove = false
t.userdef = true
t.template = "cbi/tblsection"
t.paging = true
-- if #user_list > 0 then
--     t.paging = true
-- else
--     t.paging = false
-- end




t.server_page = true
if (nac_run > 0) then
	t.purl =  luci.dispatcher.build_url("admin/ac_nac/onlineuser/onlineuser_display")
end



t.pdata_name = "users"
t.template_aoColumnDefs = "nac/online_users"
t.cur_url = ds.build_url("")
t.confirm = translate("Are you sure you want to force the user off the assembly line?")


t:option(DummyValue, "name", translate("User name"))
t:option(DummyValue, "userip", translate("User IP"))

op = t:option(DummyValue, "usermac", translate("User MAC"))
-- function op.cfgvalue(self,section)
-- 	return user_list[section].usermac
-- end

op = t:option(DummyValue, "upflow", translate("out"))
-- function op.cfgvalue(self, section, scope)
--     local obj = user_list[section]
--     return nt.format_traffic(obj["upflow"]) 
-- end

op = t:option(DummyValue, "downflow", translate("in"))
-- function op.cfgvalue(self, section, scope)
--     local obj = user_list[section]
--     return nt.format_traffic(obj["downflow"]) 
-- end

op = t:option(DummyValue, "life", translate("Session time"))
-- function op.cfgvalue(self, section)
--    return nt.format_life(user_list[section].life)
-- end

op = t:option(DummyValue, "state", translate("state"))
-- function op.cfgvalue(self, section)
--     local str = ""
--     if user_list[section].state == 0 then
--         str = str .."Unauthorized"
--     elseif user_list[section].state == 1 then
--         str = str .. "Authenticating"
--     elseif user_list[section].state == 2 then
--         str = str .. "OK"
--     elseif user_list[section].state == 3 then
--         str = str .. "Overfulfil"
--     elseif user_list[section].state == 4 then
--         str = str .. "Waiting"
--     end
--    return translate(str)
-- end

-- 下线
e = t:option(Button, "remove", translate("operation"))
-- e.inputstyle = "remove"
-- -- 自定义按钮
-- e.render = function(self, section, scope)
--     e.template  = "nac/btn_delete"
--     e.name = user_list[section].name
--     e.usermac = user_list[section].usermac
--     e.userip = user_list[section].userip
--     e.title = "Drop"
--     e.info = translate("Drop")
--     -- 下一个URL
--     e.nurl = ds.build_url("admin/ac_nac/onlineuser_drop", e.name).. "?usermac=" .. e.usermac .. "&userip=" .. e.userip
--     e.confirm = translate("Are you sure you want to force the user off the assembly line?")
--     Button.render(self, section, scope)
-- end


return m
